﻿using Assignment_3.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment_3.Feature___Housekeeping
{
    public partial class UpdateSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            if (!IsPostBack)
            {
                string id = Request.QueryString["housekeepingID"];

                Object[] housekeeping = DatabaseManager.GetHouseKeepingById(Convert.ToInt32(id));
                lblScheduleID.Text = Convert.ToString((int)housekeeping[0]);
                lblStaffID.Text = Convert.ToString((int)housekeeping[1]);
                tbxRoom.Text = Convert.ToString((int)housekeeping[4]);
                DateTime date = (DateTime)housekeeping[5];
                tbxDate.Text = string.Format("{0:yyyy-MM-dd}", date);
                lblProgress.Text = (string)housekeeping[6];
            }
            CalendarExtender1.StartDate = DateTime.Now;
            CalendarExtender1.EndDate = DateTime.Now.AddMonths(1);
            tbxDate.Attributes.Add("ReadOnly", "true");
        }

        protected void tbnUpdate_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(tbxRoom.Text);

            if (DatabaseManager.compareRoomID(id) == false)
            {
                lblCheckRoomID.Text = "There is no Room with that ID!";

            }

            else if (DatabaseManager.compareRoomStatus(id) == false)
            {
                lblCheckRoomID.Text = "This room's status is not on Vacant & Cleaning!";
            }

            else
            {
                HouseKeeping schedule = new HouseKeeping();
                schedule.HouseKeepingID = Convert.ToInt32(lblScheduleID.Text);
                schedule.StaffID = Convert.ToInt32(lblStaffID.Text);
                schedule.RoomID = Convert.ToInt32(tbxRoom.Text);
                schedule.Date = Convert.ToDateTime(tbxDate.Text);
                schedule.Progress = lblProgress.Text;

                if (DatabaseManager.UpdateHouseKeeping(schedule) != 0)
                {
                    Response.Redirect("UpdateScheduleStatus.aspx?housekeepingID=" + schedule.HouseKeepingID);
                }

                else
                {
                    lblSuccess.Text = "Schedule is NOT successfully updated. Please check again!";
                }
            }
        }

        protected void tbnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("RetrieveSchedule.aspx?");
        }
    }
}